package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemLongClickListener;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.ImitationDrawActivity;
import flc.ast.activity.NewDrawActivity;
import flc.ast.adapter.CreativeRecordAdapter;
import flc.ast.bean.MyDrawBean;
import flc.ast.bean.MyRecordBean;
import flc.ast.databinding.FragmentHomeBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private MyDrawBean curBean;
    private List<MyRecordBean> listShow = new ArrayList<>();
    private CreativeRecordAdapter recordAdapter;
    private int oldPosition;

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivCreateDraw.setOnClickListener(this);
        mDataBinding.ivImitationDraw.setOnClickListener(this);
        mDataBinding.ivFunFilling.setOnClickListener(this);
        mDataBinding.tvStartFilling.setOnClickListener(this);
        mDataBinding.tvRecordClear.setOnClickListener(this);

        mDataBinding.rvRecordList.setLayoutManager(new GridLayoutManager(mContext, 2));
        recordAdapter = new CreativeRecordAdapter();
        mDataBinding.rvRecordList.setAdapter(recordAdapter);
        recordAdapter.addChildClickViewIds(R.id.ivCreativeRecordItemDelete);
        recordAdapter.addChildClickViewIds(R.id.ivCreativeRecordItemCancel);
        recordAdapter.setOnItemChildClickListener(this);
        recordAdapter.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                recordAdapter.getItem(oldPosition).setSelected(false);
                recordAdapter.notifyItemChanged(oldPosition);
                oldPosition = position;
                recordAdapter.getItem(position).setSelected(true);
                recordAdapter.notifyItemChanged(position);
                return false;
            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivCreateDraw://新建画布
                NewDrawActivity.isNew = true;
                NewDrawActivity.isFill = false;
                startActivity(NewDrawActivity.class);
                break;
            case R.id.ivImitationDraw://临摹绘画
                ImitationDrawActivity.isImitation = true;
                startActivity(ImitationDrawActivity.class);
                break;
            case R.id.ivFunFilling://趣味填色
                ImitationDrawActivity.isImitation = false;
                startActivity(ImitationDrawActivity.class);
                break;
            case R.id.tvStartFilling://开始填色
                NewDrawActivity.isNew = false;
                NewDrawActivity.drawBean = curBean;
                NewDrawActivity.isFill = true;
                startActivity(NewDrawActivity.class);
                break;
            case R.id.tvRecordClear://清空
                if (listShow.size() == 0) {
                    ToastUtils.showShort(getString(R.string.no_data_text));
                    return;
                }
                clear();
                break;
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()) {
            case R.id.ivCreativeRecordItemDelete:
                MyRecordBean bean = recordAdapter.getItem(position);
                oldPosition = 0;
                deleteFile(bean);
                break;
            case R.id.ivCreativeRecordItemCancel:
                recordAdapter.getItem(position).setSelected(false);
                recordAdapter.notifyItemChanged(position);
                break;
        }
    }

    @Override
    protected void initData() {
        String date = TimeUtils.getNowString(new SimpleDateFormat("yyyy/MM/dd"));
        mDataBinding.tvDate.setText(date);
        setData();
    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    private void setData() {
        List<MyDrawBean> list = new ArrayList<>();
        list.add(new MyDrawBean(R.drawable.aa1, R.drawable.a1));
        list.add(new MyDrawBean(R.drawable.aa2, R.drawable.a2));
        list.add(new MyDrawBean(R.drawable.aa3, R.drawable.a3));
        list.add(new MyDrawBean(R.drawable.aa4, R.drawable.a4));
        list.add(new MyDrawBean(R.drawable.aa5, R.drawable.a5));
        list.add(new MyDrawBean(R.drawable.aa6, R.drawable.a6));
        list.add(new MyDrawBean(R.drawable.aa7, R.drawable.a7));
        list.add(new MyDrawBean(R.drawable.aa8, R.drawable.a8));
        Calendar c = Calendar.getInstance();
        int day = c.get(Calendar.DAY_OF_MONTH);
        curBean = list.get(day % 8);
        mDataBinding.ivSmallImg.setImageResource(curBean.getSmallImg());
        mDataBinding.ivBigImg.setImageResource(curBean.getBigImg());
    }

    private void getData() {
        listShow.clear();
        //获取文件夹下文件
        String path = PathUtils.getFilesPathExternalFirst() + Content.myCreativeRecord;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                listShow.add(new MyRecordBean(file.getPath(), false));
            }
        }
        if (listShow.size() > 0) {
            recordAdapter.setList(listShow);
            mDataBinding.rvRecordList.setVisibility(View.VISIBLE);
            mDataBinding.tvRecordNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvRecordList.setVisibility(View.GONE);
            mDataBinding.tvRecordNoData.setVisibility(View.VISIBLE);
        }
    }

    //删除
    private void deleteFile(MyRecordBean bean) {
        showDialog(getString(R.string.delete_ing));
        RxUtil.create(new RxUtil.Callback<String>() {

            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                FileUtils.delete(bean.getPath());
                emitter.onNext("");
            }

            @Override
            public void accept(String path) {
                dismissDialog();
                ToastUtils.showShort(getString(R.string.delete_suc));
                getData();
            }
        });
    }

    //清空
    private void clear() {
        showDialog(getString(R.string.delete_ing));
        RxUtil.create(new RxUtil.Callback<String>() {

            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                for (MyRecordBean bean : listShow) {
                    FileUtils.delete(bean.getPath());
                }
                emitter.onNext("");
            }

            @Override
            public void accept(String path) {
                dismissDialog();
                ToastUtils.showShort(getString(R.string.delete_suc));
                getData();
            }
        });
    }

}